package com.github.tvbox.osc.ui.activity.Media

import android.os.Build
import android.text.Html
import android.view.View
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.github.tvbox.osc.R
import com.orhanobut.logger.Logger
import io.github.source.config.VodConfig
import io.github.source.media.bean.MediaBean

/**
 * @ClassName: MediaBaseView
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author: huangrongcai
 * @date: 2024/8/22
 *
 * @Copyright: 黄嵘才（HuangRc)
 */
class MediaBaseView {

    private var mView: View? = null

    // 用于缓存视图，减少重复查找
    private val viewCache = HashMap<Int, View>()
    private var mTvYear: TextView? = null
    private var mTvSite: TextView? = null
    private var mTvArea: TextView? = null
    private var mTvLang: TextView? = null
    private var mTvType: TextView? = null
    private var mTvActor: TextView? = null
    private var mTvDirector: TextView? = null
    private var mTvDes: TextView? = null




    companion object {

        @JvmStatic
        fun create(fActivity: FragmentActivity, view: View?): MediaBaseView {
            return MediaBaseView(view)

        }
        /**
         * 创建实例
         *
         * @param view 界面父类的View
         * @return
         */
        @JvmStatic
        fun create(fragment: Fragment, view: View?): MediaBaseView {
            return MediaBaseView(view)
        }

    }

    constructor(view: View?){
        mView = view
        mTvYear = findViewById<TextView>(R.id.skin_details_Year)
        mTvSite = findViewById<TextView>(R.id.skin_details_Site)
        mTvArea = findViewById<TextView>(R.id.skin_details_Area)
        mTvLang = findViewById<TextView>(R.id.skin_details_Lang)
        mTvType = findViewById<TextView>(R.id.skin_details_Type)
        mTvActor = findViewById<TextView>(R.id.skin_details_Actor)
        mTvDirector = findViewById<TextView>(R.id.skin_details_Director)
        mTvDes = findViewById<TextView>(R.id.skin_details_Des)
    }

    private fun <T : View?> findViewById(@IdRes id: Int): T? {
        // 尝试从缓存获取，如果缓存中没有，则从视图树中查找并加入缓存
        return if (mView != null) {
            mView?.findViewById(id)
        } else {
            Logger.e("View is not initialized or null.")
            null
        }
    }

    private fun safeFindViewById(@IdRes id: Int): View? {
        return if (mView != null) {
            mView?.findViewById(id)
        } else {
            Logger.e("View is not initialized or null.")
            null
        }
    }

    fun getView(): View? {
        return mView
    }

    fun setMediaDetails(mediaBean: MediaBean) {
        setMediaSite(VodConfig.get().getSite(mediaBean.siteKey).name)
        setMediaYear(if (mediaBean.vodYear == null) "" else mediaBean.vodYear)
        setMediaArea(mediaBean.vodArea)
        setMediaLang(mediaBean.vodPlayFrom)
        setMediaType(mediaBean.typeName)
        setMediaActor(mediaBean.vodActor)
        setMediaDirector(mediaBean.vodDirector)
        setMediaDes(removeHtmlTag(mediaBean.vodContent))
    }

    fun setMediaSite(info: CharSequence?) {
        setTextShow(mTvSite, "来源：", info)
    }

    fun setMediaArea(info: CharSequence?) {
        setTextShow(mTvArea, "来源：", info)
    }

    fun setMediaYear(info: CharSequence?) {
        setTextShow(mTvYear, "年份：", info)
    }

    fun setMediaLang(info: CharSequence?) {
        setTextShow(mTvLang, "语言：", info)
    }

    fun setMediaType(info: CharSequence?) {
        setTextShow(mTvType, "类型：", info)
    }

    fun setMediaActor(info: CharSequence?) {
        setTextShow(mTvActor, "演员：", info)
    }

    fun setMediaDirector(info: CharSequence?) {
        setTextShow(mTvDirector, "导演：", info)
    }

    fun setMediaDes(info: CharSequence?) {
        setTextShow(mTvDes, "内容简介：", info)
    }

    private fun setTextShow(view: TextView?, tag: String?, info: CharSequence?) {
        if (view == null) {
            return
        }
        // 对tag也进行非空和非空格检查
        if (tag == null || tag.trim { it <= ' ' }.isEmpty() || info == null || info.toString()
                .trim { it <= ' ' }
                .isEmpty()
        ) {
            view.visibility = View.GONE
            return
        }
        view.visibility = View.VISIBLE
        // 使用更新的fromHtml方法，并添加兼容性处理
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            view.text = Html.fromHtml(tag + info, Html.FROM_HTML_MODE_COMPACT)
        } else {
            view.text = Html.fromHtml(tag + info)
        }
    }

    private fun removeHtmlTag(info: String?): String? {
        return info?.replace("\\<.*?\\>".toRegex(), "")?.replace("\\s".toRegex(), "") ?: ""
    }

    fun isSetMediaDetails(): Boolean {
        return mView != null
    }

    /**
     * 是不是详情页准备准备好
     * @return true 准备
     */
    fun isDetailsReady(): Boolean {
        return mView != null
    }

}